home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / cug106 / stdef.c < prev    next >
Text File  |  1984-06-14  |  6KB  |  239 lines

  1. /*    standard definition library file
  2. **
  3. **    Version 1.3    16-Aug-80
  4. */
  5.  
  6. char               strbuf[256];        /* string buffer */
  7. #define SBP         &strbuf[0]
  8.  
  9. #define    BDOS_ERR    255
  10. #define    CPM_EOF        26
  11. #define    DIGIT        0
  12. #define    EOF        -1
  13. #define    EQUAL        0
  14. #define    ERROR        -1
  15. #define    FALSE        0
  16. #define    K        1024
  17. #define    NO         0
  18. #define    RESET        0
  19. #define    SECTOR      128
  20. #define    SET        -1
  21. #define TRUE        -1
  22. #define    U_CASE        95
  23. #define    YES        1
  24.  
  25. #define    ALPHA         'a'
  26. #define    ANY         '?'
  27. #define    ASCII         48
  28. #define    BELL         7
  29. #define    CR         '\r'
  30. #define    D_QUOTE         '"'
  31. #define    EOS         '\0'
  32. #define    ESCAPE         27
  33. #define    FORM_FEED     12
  34. #define    LF         '\n'    
  35. #define    NEWLINE         '\n'
  36. #define NULL        '\0'
  37. #define    QUOTE         39
  38. #define    SPACE         ' '
  39. #define    TAB         '\t'
  40. ns are illegal and must
  41.           be broken into steps.
  42.  
  43.     IMPORTANT: in order to communicate with the external
  44.            work buffer, all separately compiled files
  45.            should contain the following declarations
  46.            as the first order of business.
  47.  
  48.            ch;                strfun.asm
  49. ;
  50. ;    Copyright (C) 1980, M J Maney
  51. ;
  52. ;    First created 8/25/80
  53. ;    Last revised 8/30/80 19:15
  54. ;
  55. ;    Herein are the basic string functions that C people like to use,
  56. ;    coded in assembler to make them as fast as can be.
  57. ;
  58. ;    NOTE that I have used .NE. in the comments in place of the usual C
  59. ;    symbol, because MAC sometimes objects to such usage.
  60. ;
  61.     maclib    crl
  62. ;
  63. ;
  64. ; * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  65. ;
  66. ;                string functions
  67. ;
  68. ;    strlen        strcmp        strcpy        strcat
  69. ;
  70. ; * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  71. ;
  72. ;
  73. ;    strlen(str)
  74. ;    char *str;
  75. ;
  76. ;    Returns the length of the string pointed to by str. It is assumed
  77. ;    that the string is terminated by '\0', and the terminator is NOT
  78. ;    counted as a character, so the storage length of a string s is
  79. ;    strlen(s) + 1.
  80. ;
  81.     PROC    STRLEN
  82.     lxi    d,0
  83.     lhld    ARG1        ;str to HL
  84.     mvi    a,0
  85. strlen1    cmp    m
  86.     BZ    strlen2        ;for (i=0; *str++ .NE. EOS; i++) ;
  87.     inx    h
  88.     inx    d
  89.     BRA    strlen1
  90. ;
  91. strlen2    xchg            ;return i;
  92.     ret
  93.     PEND    STRLEN
  94. ;
  95. ;
  96. ;    strcmp(sx,sy)
  97. ;    char *sx,*sy;
  98. ;
  99. ;    Returns 1, 0, or -1, depending upon the lexicographical relation
  100. ;    of the strings pointed to by sx and sy. The ordering is essentially
  101. ;    that which would be used if the strings were words being alphebetized
  102. ;    except that upper case and lower case are distinct, and digits and
  103. ;    punctuation are treated as significant characters. The relation
  104. ;    between any two characters is simply the relation between the bit
  105. ;    patterns that are used to represent them, treated as unsigned
  106. ;    integers. It is also assumed that the string terminator, '\0', has
  107. ;    a smaller value than any other character (in fact, its zero).
  108. ;
  109. ;    1 if sx > sy, 0 if sx == sy, or -1 if sx < sy
  110. ;
  111.     PROC    STRCMP
  112.     lhld    ARG1
  113.     xchg            ;sx to DE
  114.     lhld    ARG2        ;sy to HL
  115. ;
  116. strcmp1    ldax    d
  117.     cmp    m        ;while (*sy == (c = *sx)) {
  118.     BNZ    strcmp2
  119.     inx    h        ;    sy++;
  120.     inx    d        ;    sx++;
  121.     ora    a
  122.     BNZ    strcmp1        ;    if (c == EOS)
  123.     lxi    h,0        ;        return 0;
  124.     ret            ;    }
  125. strcmp2    lxi    h,1        ;if (*sx > *sy)
  126.     rnc            ;    return 1;
  127.     dcx    h        ;else
  128.     dcx    h
  129.     ret            ;    return -1;
  130.     PEND    STRCMP
  131. ;
  132. ;
  133. ;    strcpy(des,src)
  134. ;    char *des,*src;
  135. ;
  136. ;    Copies string pointed to by src into memory starting at des. No
  137. ;    provision is made for limiting the amount of stuff copied: it is
  138. ;    the user's responsibility to insure that the string being copied
  139. ;    will fit where it is being copied to.
  140. ;
  141.     PROC    STRCPY
  142.     lhld    ARG1
  143.     xchg            ;des to DE
  144.     lhld    ARG2        ;src to HL
  145. strcpy1    mov    a,m        ;do {
  146.     inx    h
  147.     stax    d
  148.     inx    d        ;    *des++ = c = *src++;
  149.     ora    a
  150.     BNZ    strcpy1        ;    }while (c .NE. EOS);
  151.     ret            ;return;
  152.     PEND    STRCPY
  153. ;
  154. ;
  155. ;    strcat(des,src)
  156. ;    char *des,*src;
  157. ;
  158. ;    Copies the string pointed to by src onto the tail end of the string
  159. ;    pointed to by des. This is equivalent to
  160. ;
  161. ;        strcpy(des+strlen(des),src)
  162. ;
  163. ;    and there is an identical lack of testing for overrun during the
  164. ;    copying process.
  165. ;
  166.     PROC    STRCAT
  167.     lhld    ARG2
  168.     xchg            ;src to DE
  169.     lhld    ARG1        ;des to HL
  170.     sub    a
  171. strcat1    cmp    m        ;while (*des++ .NE. EOS)
  172.     inx    h        ;    ;
  173.     BNZ    strcat1
  174.     dcx    h        ;des--;
  175. strcat2    ldax    d        ;do {
  176.     inx    d
  177.     mov    m,a
  178.     inx    h        ;    *des++ = c = *src++;
  179.     ora    a
  180.     BNZ    strcat2        ;    }while (c .NE. EOS);
  181.     ret            ;return;
  182.     PEND    STRCAT
  183. ;
  184. ;
  185.     end
  186. P;
  187. }
  188.  
  189. insert(s,is,fpos)
  190. int fpos;
  191. char *s, *is;
  192. {
  193.     char *tp; tp = SBP;
  194.  
  195.     while (--fpos != 0) *tp++ = *s++;
  196.     while (*tp++ = *is++); --tp;
  197.     while (*tp++ = *s++); return SBP;
  198. }
  199.     
  200. left(s,n)
  201. int n;
  202. char *s;
  203. {
  204.     char *tp; tp = SBP;
  205.  
  206.     while (n-/*                stdio.src
  207.  
  208.     Copyright (C) 1980, M J Maney
  209.  
  210.     First created    3/15/80
  211.     Last revised    4/19/80 1:45
  212.  
  213.     This file defines some useful functions that are designed to help
  214.     make BDS C programs running under CP/M pretend that they're
  215.     running in a nicer environment (like UNIX, ha-ha). It makes it
  216.     easier to setup and use STDIN and STDOUT directed IO, and
  217.     although you can't just assume that they're there as you can
  218.     with UNIX C, at least you don't have to put in code to parse
  219.     the command line into each program you write: just use the
  220.     functions here.
  221. */
  222.  
  223.  
  224. #include "csym.lib"
  225. #include "stdio.lib"
  226.  
  227. #define ESCAPE '\\'
  228.  
  229.  
  230. /* stdopen parses the command line arguments looking for arguments that
  231.     begin with "<" or ">", which are respectively taken to be the
  232.     filenames for STDIN and STDOUT. The defaults are to use the
  233.     console for both STDIN and STDOUT. Any argument beginning with
  234.     "<" or ">" is effectively removed from the argument list (by
  235.     moving pointers in argv[] and decreasing argc)...if your program
  236.     wants to get an argument beginning with "<" or ">", you must
  237.     type it as "\<" or "\>", and stdopen will replace the escape
  238.     sequence in the strings, but will not "use" those arguments
  239.     for IO assignment. If you want to pass arguments beg